package com.kushe.dao;

import com.kushe.config.Config;
import com.kushe.entity.Message;
import com.kushe.entity.Reply;
import com.kushe.util.core.DataAccess;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;

import java.util.Date;
import java.util.List;

/**
 * Created by WhiteBlue on 15/5/8.
 */
public class MessageDao extends DataAccess<Message> {

    public Message get(int id) {
        return getDao().fetch(Message.class, id);
    }


    /**
     * 列出某用户的所有消息
     *
     * @param user_id 用户id
     */
    public QueryResult listMessage(int user_id, int page) {
        Pager pager = getDao().createPager(page, Config.PAGE_SIZE);
        List<Message> list = getDao().query(Message.class, Cnd.where("from_id", "=", user_id).or("to_id", "=", user_id).desc("time"), pager);
        return new QueryResult(list, pager);
    }


    /**
     * 得到首页的私信
     *
     * @param user_id 用户id
     */
    public List<Message> listNew(int user_id) {
        return getDao().query(Message.class, Cnd.where("from_id", "=", user_id).and("read_A", "=", false).or("to_id", "=", user_id).and("read_B","=",false).desc("time"));
    }


    /**
     * 得到消息对应的回复列表
     *
     * @param messageId  消息的id
     * @param pageNumber 页码
     */
    public QueryResult listReply(int messageId, int pageNumber, int user_id) {
        Message message = getDao().fetch(Message.class, messageId);

        if ((message.getFrom_id() == user_id) || (message.getTo_id() == user_id)) {
            if (user_id == message.getFrom_id()) {
                message.setRead_A(true);
                getDao().update(message);
            } else {
                message.setRead_B(true);
                getDao().update(message);
            }

            Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
            List<Reply> list = getDao().query(Reply.class, Cnd.where("message_id", "=", messageId), pager);
            return new QueryResult(list, pager);
        } else {
            return null;
        }

    }

    public boolean haveMessage(int fromId, int toId) {
        if (getDao().count(Message.class, Cnd.where("from_id", "=", fromId).and("to_id", "=", toId)) > 0) {
            return true;
        } else {
            return false;
        }
    }

    public Message createMessage(int fromId, int toId, String content, Date time) {
        Message message = new Message();
        message.setFrom_id(fromId);
        message.setTo_id(toId);
        message.setRead_A(true);
        message.setRead_B(true);
        getDao().insert(message);

        Reply reply = new Reply();
        reply.setFrom_id(fromId);
        reply.setMessage_id(message.getId());
        getDao().insert(reply);
        return message;
    }

    public Message addReply(int fromId, int toId, String content, Date time) {
        try {
            Message message = getDao().fetch(Message.class, Cnd.where("from_id", "=", fromId).and("to_id", "=", toId));
            //给message设置未读
            if (fromId == message.getFrom_id()) {
                message.setRead_B(false);
                getDao().update(message);
            } else {
                message.setRead_A(false);
                getDao().update(message);
            }
            Reply reply = new Reply();
            reply.setFrom_id(fromId);
            reply.setMessage_id(message.getId());
            reply.setContent(content);
            reply.setTime(time);
            getDao().insert(reply);
            return message;
        } catch (Exception e) {
            return null;
        }
    }

}
